extends CharacterBody2D

const RUNSPEED:=200.0
const JUMPVECTORY:=-600
@onready var area_2d: Area2D = $Area2D


var gravity:=ProjectSettings.get("physics/2d/default_gravity")as float

@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _ready() -> void:
	area_2d.add_to_group("player")
	

func _physics_process(delta: float) -> void:
	var direction:=Input.get_axis("left","right")
	velocity.x=direction*RUNSPEED
	velocity.y+=gravity*delta
	
	if is_on_floor() and Input.is_action_just_pressed("jump"):
		velocity.y=JUMPVECTORY
		
	if is_on_floor():
		if is_zero_approx(direction):
			animation_player.play("idle")
		else:
			animation_player.play("run")
	else:
		animation_player.play("jump")
			
	if not is_zero_approx(direction):
		
		sprite_2d.flip_h=direction>0
	move_and_slide()
